//
//  servicePlayListDemo.m
//  wAudioShare
//
//  Created by 赵帅 on 14-1-13.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "servicePlayListDemo.h"

@implementation servicePlayListDemo
-(id)initWithDelegate:(id <servicePlayListDemoDelegate>) aDelegate
{
	self = [super init];
	if (self)
	{
		self.m_serviceHandler = aDelegate;
	}
    
	return self;
}



#pragma mark -
#pragma mark Configure the request
-(NSURL*)targetURL
{
    NSString * musicurl = [m_dicSendData objectForKey:@"baseurl"];
    NSArray * devices = [m_dicSendData objectForKey:@"devices"];
    NSString * masterIP = [m_dicSendData objectForKey:@"master"];
    
    NSString *urlStr = nil;
    if([devices count] != 0)
    {
        NSMutableString * ipStr = [NSMutableString string];
        
        for(NSString * str in devices)
        {
            [ipStr appendFormat:@":%@",str];
        }
        
        urlStr = [NSString stringWithFormat:@"http://%@/httpapi.asp?command=setPlayerCmd:playlistdemo:%@[wiimu_multiroom_master%@]",masterIP,musicurl,ipStr];
    }
    else
    {
        urlStr = [NSString stringWithFormat:@"%@/httpapi.asp?command=setPlayerCmd:playlistdemo:%@",masterIP,musicurl];
    }
    
    NSLog(@"play local music:%@",urlStr);
    
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	NSURL *theUrl = [[NSURL alloc] initWithString:urlStr];
	
	return theUrl;
}

-(void)configureRequest
{
	[super configureRequest];
	[m_request setRequestMethod:@"GET"];
}

#pragma mark -
#pragma mark Parse the response
-(void)parseResponseData:(NSData *)data
{
	[super parseResponseData:data];
    
	NSMutableString * retStr = [NSMutableString stringWithString:m_request.responseString];
	
    if(retStr == nil)
    {
        [m_dicReceiveData setObject:SERVICE_FAIL forKey:@"responsestatus"];
        return;
    }
}

#pragma mark -
#pragma mark Pass Data To UI
-(void)passDataOut
{
	[super passDataOut];
	
	if (nil != self.m_serviceHandler
		&& [self.m_serviceHandler respondsToSelector:@selector(servicePlayListDemoCallBack:)])
	{
        [self.m_serviceHandler performSelectorOnMainThread:@selector(servicePlayListDemoCallBack:) withObject:m_dicReceiveData waitUntilDone:YES];
	}
}

@end
